<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\Adboard;
use app\common\model\AdboardCate;
use app\common\model\Ad;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class AdminAdvert extends AdminInit
{
    public function __construct()
    {
        parent::__construct();
        $list = Adboard::select();
        View::assign("list",$list);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $keyword = request()->param("keyword");
        $search_field = request()->param("search_field");
        if($keyword && $search_field){
            if ($search_field == 'id'){
                $where[] = ["{$search_field}",'=',"{$keyword}"];
            }else{
                $where[] = ["{$search_field}",'like',"%{$keyword}%"];
            }
        }
        if(!isset($where)){
            $where = 1;
        }
        $list = Ad::with(['b'])->where($where)
            ->order("id DESC")
            ->paginate(["query"=>request()->param()]);
        //echo Ad::getlastsql();die;
        View::assign("list",$list);
        View::assign("title","文章列表");
        return  view();
    }



    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        View::assign("res",[]);
        return \view("edit");
    }



    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        if(Request::isAjax()){
            $d = input("post.");
            if(isset($d["id"])){
                $res = Ad::update($d);
            }else{
                $res = Ad::create($d);
            }

            if($res){
                return  json(["code"=>200,"msg"=>"成功"]);
            }else{
                return  json(["code"=>4000,"msg"=>"请稍后再试"]);
            }
        }

        $id = input("get.id");
        $res = Ad::find($id);
        View::assign("res",$res);
        return view();
    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = Ad::destroy($id);
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }

}
